var ${objectName}FormName = 'searchForm${entityName}';
var ${objectName}DgId = 'dataGrid${entityName}';
var delete${entityName}URL = '${serviceName}/${objectName}/delete.action';
var edit${entityName}URL = '${serviceName}/${objectName}/update.action';
var ${objectName}DgIdEditHtmlURL = '${serviceName}/${serviceName}/${objectName}/html/${objectName}Edit.html';

var exportTemplateURL = '${serviceName}/${objectName}/exportTemplate.action';
var exportAllDataURL = '${serviceName}/${objectName}/exportAllData.action';
var exportCurrentPageDataURL = '${serviceName}/${objectName}/exportCurrentPageData.action';
var importDataURL = '${serviceName}/${objectName}/importData.action';
var exportExcelName = '${entityComment}';

/**
* 用于判断导入的文件是否为手动清除的
* 手动清除的，则不抛异常
*/
var clearFlag = false;

erajs.onReady(function(){
    // 查询界面码值
    // fams.getComboboxData('#searchForm${entityName}', ['EquityFactorRule_AssetClass']);
    // 编辑界面保存后使用
    currentDataGrid = erajs.getCmp(${objectName}DgId);
});

/**
 * 点击查询按钮
 */
function clickQuery(){
    var dgCmp = erajs.getCmp(${objectName}DgId);
    dgCmp.reload();
}

/**
* 新增${entityComment}
*/
function add${entityName}() {
    let url = erajs.baseURL() + ${objectName}DgIdEditHtmlURL;
    let options = {
        height: 200,
        width: 750,
        openWith: 'inside-iframe',
        maximizable: false
    };
    let params = {
        action: 'add'
    };
    erajs.Extra.openNewWindow(erajs.merge(options,{
        height: 520,
        width: 780,
        id: '${objectName}',
        title: erajs.getMessage('common.window.add'),
        href: erajs.String.urlAppend(url, erajs.Object.toQueryString(params))
    }));
}

/**
* 修改${entityComment}
*/
function edit${entityName}() {
    let row = erajs.getCmp(${objectName}DgId).getSelected();
    if (!fams.unselectLessOneRowCheck(row)) {
        return false;
    }
    let url = erajs.baseURL() + ${objectName}DgIdEditHtmlURL;
    let options = {
        height: 200,
        width: 750,
        openWith: 'inside-iframe',
        maximizable: false
    };
	// <todo> 改为主键
    let params = {
        action: 'edit',
        primaryKey: row['primaryKey']
    };
    erajs.Extra.openNewWindow(erajs.merge(options,{
        height: 520,
        width: 780,
        id: '${objectName}',
        title: erajs.getMessage('common.window.edit'),
        href: erajs.String.urlAppend(url, erajs.Object.toQueryString(params))
    }));
}

/**
* 删除${entityComment}
*
*/
function delete${entityName}() {
	let row = erajs.getCmp(${objectName}DgId).getSelected();
	if (!fams.unselectLessOneRowCheck(row)) {
		return false;
	}
	let maskDivId = '${objectName}_query_layout';
	fams.confirmOperation(function (r) {
		if (r) {
			let url = erajs.baseURL() + delete${entityName}URL;
			// <todo> 改为主键
			fams.executeRemoteRequest(maskDivId, url, {primaryKey:row['primaryKey']}, function (data, er${serviceName}g) {
				if (data) {
					erajs.Extra.showInfo(erajs.getMessage('framework.EXECUTE_SUCCESS'));
					erajs.getCmp(${objectName}DgId).reload();
				}
				return true;
			});
		}
	});
}



/**
 * 导出模板
 * @authors scb
 * @createDate 2023/05/11
 */
function exportTemplate() {
    let action = erajs.baseURL() + exportTemplateURL;
    let params = "?exportExcelName=" + exportExcelName;
    window.location.href = action + params;
}

/**
 * 导出全部数据处理
 * @authors scb
 * @createDate 2023/05/11
 */
function exportAllData() {
    let action = erajs.baseURL() + exportAllDataURL;
    let params = "?exportExcelName=" + exportExcelName;
    window.location.href = action + params;
}

/**
 * 导出当前页面数据
 * @authors scb
 * @createDate 2023/05/11
 */
function exportCurrentPageData() {
    var datagrid = erajs.getCmp(${objectName}DgId);
    var data = erajs.Extra.getFormSerializeValues($("#" + ${objectName}FormName));
    var pager = datagrid.pager;
    if (pager) {
        var pageNumber = pager.options.pageNumber;
        var pageSize = pager.options.pageSize;
        data.pageStart = pageNumber;
        data.pageLimit = pageSize;
    }
    let action = erajs.baseURL() + exportCurrentPageDataURL;
    // todo 这里要传递相应的参数
    let params = "?pageStart=" + data.pageStart + "&pageLimit="+ data.pageLimit + "&param=" + data.param + "&exportExcelName=" + exportExcelName;
    window.location.href = action + params;
}

/**
 * 打开导入界面
 * @authors scb
 * @createDate 2023/05/11
 */
function importData() {
    var selector = $('#file').parent().find('a[xtype="button"]');
    selector.click();
}


/**
 * 隐藏导入界面并传递导入文件调用导入接口
 * @authors scb
 * @createDate 2023/05/11
 */
function importFileChange() {
    let fileCheck = fileSelectCheck();
    if (!fileCheck) {
        return false;
    }
    let formData = new FormData(document.forms.namedItem('importForm'));
    let url = erajs.baseURL() + importDataURL; // 接收上传文件的后台地址
    $.ajax({
        url: url,
        type: 'POST',
        async: false, //同步
        data: formData,
        // 告诉jQuery不要去处理发送的数据
        processData: false,
        // 告诉jQuery不要去设置Content-Type请求头
        contentType: false,
        success: function success(data) {
            if (data.success === true) {
                fileReset();
                erajs.Extra.showInfo(erajs.getMessage('framework.EXECUTE_SUCCESS'));
                erajs.getCmp(rmsSceneDefineDgId).reload();
            } else {
                fileReset();
                erajs.Extra.showWarnInfo(erajs.getMessage(data.message));
            }
        },
        error: function error(data) {
            fileReset();
            erajs.Extra.showWarnInfo(erajs.getMessage(data.message));
        }
    });
}

/**
 * 导入文件检查是否为xlsx格式
 * 校验是否为空，且如果是（判单是否为手动清楚的，是？则不抛异常:抛异常）
 */
function fileSelectCheck() {
    let file = erajs.getCmp('file').getValue();
    if (file == undefined || file == '' || file == null) {
        if (clearFlag) {
            clearFlag = false;
            return false;
        } else {
            erajs.Extra.showInfo(erajs.getMessage('error.file.mustSelect'));
            return false;
        }
    }
    if (file.lastIndexOf('xlsx') != file.length - 4) {
        erajs.Extra.showInfo(erajs.getMessage('error.file.xlsx'));
        return false;
    }
    return true;
}

/**
* 将导入的文件手动重置并修改表示clearFlag = true
*/
function fileReset() {
    clearFlag = true;
    erajs.getCmp('file').clear();
}